<template>
  <div>
    <div>
      <el-card class="box-card" style="margin-bottom: 35px">
        <el-form ref="courier" :model="courier" label-width="100px">
          <el-row type="flex" justify="space-between">
            <el-form-item label="快递员账号:">
              <el-input
                v-model="courier.account"
                placeholder="请输入快递员账户"
              />
            </el-form-item>
            <el-form-item label="快递员姓名:">
              <el-input
                v-model="courier.name"
                placeholder="请输入快递员姓名"
              />
            </el-form-item>
            <el-form-item label="快递员手机:">
              <el-input
                v-model="courier.phone"
                placeholder="请输入快递员手机号"
              />
            </el-form-item>
          </el-row>
          <el-row type="flex">
            <el-form-item label="所属机构">
              <!-- 下拉 树形图 关键select需要option 关键思路隐藏 option 直接显示自适应树状结构   -->
              <el-select ref="selectTree" v-model="selectValue" class="main-select-tree select">
                <el-option v-for="item in treeData" :key="item.id" :label="item.name" :value="value" style="display: none;" />
                <el-tree
                  ref="selecteltree"
                  class="main-select-el-tree"
                  :data="treeData"
                  node-key="id"
                  highlight-current
                  :props="defaultProps"
                  :expand-on-click-node="false"
                  @node-click="hasValue"
                />
              </el-select>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="onSearch">搜索</el-button>
              <el-button @click="clear">重置</el-button>
            </el-form-item>
          </el-row>
        </el-form>
        <!--  -->
      </el-card>
    </div>

    <div class="mian">
      <el-table :data="driverList" stripe style="width: 100%">
        <el-table-column
          label="序号"
          width="100"
          type="index"
          :index="(paramsPage.page - 1) * paramsPage.pageSize + 1"
        />
        <el-table-column prop="account" label="快递员账号" />
        <el-table-column prop="name" label="快递员姓名" />
        <el-table-column prop="mobile" label="快递员电话" />
        <el-table-column prop="agency.name" label="所属机构" />
        <el-table-column prop="workStatus" label="工作状态">
          <template v-slot="{row}">
            <span v-if="row.workStatus" class="dotRed">上班</span>
            <span v-else class="dot">休息</span>
            <span />
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right">
          <template v-slot="{row}">
            <el-button class="operate" @click="workConfig(row)">作业范围分配</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" justify="center" style="margin-top: 20px;">
        <el-pagination
          :page-sizes="[10, 20, 30, 40]"
          :page-size="paramsPage.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </div>
  </div>
</template>

<script>
import { getCourierList } from '@/api/courier'
import { getTree } from '@/api/user'

function courierFormObj() {
  return {
    account: '',
    name: '',
    phone: '',
    agentId: ''
  }
}
function paramsPageObj() {
  return {
    page: 1,
    pageSize: 10
  }
}
export default {

  data() {
    return {
      courier: courierFormObj(),
      treeData: [],
      driverList: [],
      paramsPage: paramsPageObj(),
      total: 1,
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      selectValue: '',
      value: null
    }
  },
  async created() {
    this.getList()
    const res = await getTree()
    const data = JSON.parse(res.data)
    console.log(data)
    this.treeData = data
  },
  methods: {
    // 每页显示条数发生改变时
    handleSizeChange(page) {
      this.paramsPage.pageSize = page
      this.getList()
    },
    // 点击与回车分页
    handleCurrentChange(page) {
      this.paramsPage.page = page
      this.getList()
    },
    async getList() {
      console.log(this.paramsPage)
      const { data } = await getCourierList(this.paramsPage)
      this.total = +data.counts
      this.driverList = data.items
    },
    hasValue(row) {
      this.courier.agentId = row.id
      this.selectValue = row.name
      this.$refs.selectTree.blur()
    },
    onSearch() {
      this.paramsPage = { ...this.paramsPage, ...this.courier }
      console.log(this.paramsPage)
      this.getList()

      // 复原
      this.paramsPage = paramsPageObj()
    },
    // 重置
    clear() {
      this.courier = courierFormObj()
      this.selectValue = ''
      this.getList()
    },
    // 作业分配
    workConfig(row) {
      this.$router.push(`/couierMap/${row.userId}/${row.agency.name}`)
      this.$store.commit('user/SET_NAV_STATUS', true)
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-form-item {
  width: 30% !important;
}
::v-deep .el-input,
::v-deep .el-select {
  width: 100%;
}
.operate {
  background-color: transparent;
    border: none;
    color:#419eff ;
}
::v-deep .btn-next,
::v-deep .btn-prev,
::v-deep .el-pager,
::v-deep .el-pagination button:disabled,
::v-deep .el-pager li {
  background-color: #f3f5f9;
}
::v-deep .el-input, ::v-deep .el-select,.select{
    width: 100% !important;
  }

// 状态颜色
.dot {
  &::before {
    content: "";
    position: absolute;
    top: 50%;
    transform: translate(0, -50%);
    left: -2%;
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background-color:#e15536 ;
  }
}
.dotRed {
  &::before {
    content: "";
    position: absolute;
    top: 50%;
    transform: translate(0, -50%);
    left: -2%;
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background-color:#1dc779;
  }
}

//表头居中
::v-deep .el-table_1_column_8 {
  .cell {
    text-align: center;
  }
}
::v-deep .el-table_1_column_8{
   .cell{
    text-align: center;
   }
  }

</style>
